import { queryRepairList,  actionHistoryRepairList, actionRepairAdd,actionRepairEdit,queryRepairInfo, actionRepairExport,
  queryRepairProcess,queryRepairRemind, queryRepairExamine, queryRepairAcceptance, queryProcessSpares,actionRepairStatusExamine,
  actionRepairStatusAcceptancee,queryDistrictAndDeviceTreeData
} from '../services/repair';

export default {
  namespace: 'repair',

  state: {
    remindData:[],
    processData:[],
    sparesList:{},
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
      curing_submit:false,
      plan_curing:false,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_repair_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryRepairList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    *fetch_repair_history_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(actionHistoryRepairList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // // 批量删除操作、中止
    // *action_plan_do({ payload }, { call, put }) {
    //   const response = yield call(actionPlanDo, payload);
    // },
    // 导出
    *action_repair_export({ payload }, { call, put }) {
      const response = yield call(actionRepairExport, payload);
    },


    // 新增报修
    *add_repair({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionRepairAdd,payload);
      response && (
        yield put({
          type: 'path/fetch_curing_clear',
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 报修修改
    *edit_repair({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionRepairEdit,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 获取维修详情
    *fetch_repair_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryRepairInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload:{
            ...response.data,
            repairPhoto: response.repairPhoto,
          },
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },

    // 获取维修处理详情
    *fetch_repair_handle({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryRepairProcess, payload);
      response && (yield put({
        type: 'appendProcessData',
        payload:response.data.list,
      }));
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },

    // 获取区域和设备的树结构数据
    *fetch_district_tree_data({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryDistrictAndDeviceTreeData, payload);
      response && (yield put({
        type: 'appendProcessData',
        payload:response.data.list,
      }));
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },



    // 获取维修提醒
    *fetch_repair_remind({ payload }, { call, put }) {
      const response = yield call(queryRepairRemind, payload);
      response && (yield put({
        type: 'appendDataRemind',
        payload: response.data.list ,
      }))
    },

    // 审核
    *fetch_repair_examine({ payload }, { call, put }) {
      const response = yield call(queryRepairExamine, payload);
    },

    // 验收
    *fetch_repair_acceptance({ payload }, { call, put }) {
      const response = yield call(queryRepairAcceptance, payload);
    },

    // 根据进程ID 搜索备件
    *fetch_repair_process_spares({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { [payload.processId] :true },
      });
      const response = yield call(queryProcessSpares, payload);
      response && (yield put({
        type: 'appendDataSpares',
        payload: {[payload.processId]:response.data },
      }))
      yield put({
        type: 'changeLoading',
        payload: { [payload.processId] :false },
      });
    },

    // 审核状态改变
    *fetch_repair_status_examine({ payload }, { call, put }) {
      const response = yield call(actionRepairStatusExamine, payload);
    },

    // 验收状态改变
    *fetch_repair_status_acceptance({ payload }, { call, put }) {
      const response = yield call(actionRepairStatusAcceptancee, payload);
    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    appendProcessData(state, action) {
      return {
        ...state,
        processData: action.payload,
      };
    },
    appendDataSpares(state, action) {
      return {
        ...state,
        sparesList: {
          ...state.loading,
          ...action.payload,
        },
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendDataRemind(state, action) {
      return {
        ...state,
        remindData: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        sparesList:{},
        resdata:{},
        detaildata:{},
        remindData:[],
        processData:[],
        loading:{}
      };
    },
  },

};
